package biz.ezcom.hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import biz.ezcom.hibernate.pojo.User;

public class Client {
	
	@Test
	public void testSessionSave(){	
		Session session = null;
		Transaction tx = null;
		User user = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			user = new User();	//此时user对象变为Transient状态,数据库无此对象，session也不管理
			user.setName("Felix");
			user.setPassword("password");
			user.setCreateTime(new Date(0));
			user.setExpireTime(new Date());
			
			session.save(user); //此时主键为自增(identity),发SQL,save()后user变persistent状态
										//如主键类型非DB创建,commit时再发SQL
			user.setName("Terry");
			session.save(user); //user 已经被session管理了,变成update

			tx.commit();//事务提交,发update
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
	@Test
	public void testSessionGet(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			//	Get方法查找数据库，直接发SQL,找不到返回null
			User user = (User)session.get(User.class, 1);//数据库中存在,发SQL
			if(user != null){
				System.out.println(user.getName());
			}
			user = (User)session.get(User.class, 9999);//数据库中不存在,发SQL
			if(user != null){
				System.out.println(user.getName());
			}
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
	@Test
	public void testSessionLoad(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
//			Load()方法懒加载，选返回一个CGLIB的代理对象，使用时再查找数据库，找不到抛出ObjectNotFoundException
			User user = (User)session.load(User.class, 1);//数据库中存在
			if(user != null){
				System.out.println(user.getName()); //发SQL
			}
			user = (User)session.load(User.class, 9999);//数据库中不存在
			if(user != null){
				System.out.println(user.getName()); //报org.hibernate.ObjectNotFoundException
			}
			
			tx.commit();
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
	@Test
	public void testSessionUpdate(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			//update DB 中 存在的,正常update
			User userA = (User)session.get(User.class, 1);
			System.out.println(userA.getName());
			userA.setName("NewName");
			session.update(userA);	//不发SQL

			//update DB中 不存在的,commit时报:org.hibernate.StaleStateException
			User userB = new User();
			userB.setId(9999);
			userB.setName("Felix");
			userB.setCreateTime(new Date(0));
			userB.setExpireTime(new Date());
			userB.setPassword("password");
			session.update(userB);	//不发SQL
			
			tx.commit(); //发SQL
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
	
	@Test
	public void testSessionDelete(){
		Session session = null;
		Transaction tx = null;
		try{
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			
			//delete DB中存在的记录,正常delete
			User userA = (User)session.get(User.class, 1);
			System.out.println(userA.getName());
			session.delete(userA);	//不发SQL
			
			//delete DB中不存在的记录,commit时报: org.hibernate.StaleStateException
			User userB = new User();
			userB.setId(9999);
			session.delete(userB);	//不发SQL
			
			tx.commit(); //发SQL
		}catch(Exception e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			HibernateUtil.closeSession(session);
		}
	}
}

